
$AssureDbFrameworkClass = New-PSClass AssureDbFramework -Inherit $PSSqlCommandClass {

	Constructor {
		Base $Args[0] $Args[1]
	}

  method Execute {
		$this.ExecuteNonQuery( ($Private.query) )
	}

	note -private query @"
BEGIN TRANSACTION;

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'DBFramework')
EXEC sys.sp_executesql N'CREATE SCHEMA [DBFramework] AUTHORIZATION [dbo]'
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[FileControl]') AND type in (N'U'))
BEGIN
CREATE TABLE [DBFramework].[FileControl](
	[OID] [bigint] IDENTITY(1,1) NOT NULL,
	[FilePath] [varchar](500) NOT NULL,
	[Applied] [datetime] NOT NULL,
	[CheckSum] [varchar] (100) NOT NULL
) ON [PRIMARY]
END
GO

-- Remove Old Stored Procs

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[ShouldPatchExecute]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[ShouldPatchExecute]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[MarkPatchAsExecuted]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[MarkPatchAsExecuted]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[GetAllExecutedFiles]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[GetAllExecutedFiles]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[SetFKsForTable]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[SetFKsForTable]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[TableColumns]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[TableColumns]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[LoadXML]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [DBFramework].[LoadXML]

GO


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBFramework].[Version]') AND type = N'FN')
BEGIN
EXEC dbo.sp_executesql @statement = N'
-- =============================================
-- Author:		Cash Foley cash.foley@ps.net
-- Description:	Returns Version of DBFramework
-- =============================================
CREATE FUNCTION DBFramework.Version()
RETURNS int
AS
BEGIN
	RETURN 2
END
' 
END
GO
COMMIT TRANSACTION;
"@
} 

